import {Component,OnInit,Input,Output,EventEmitter} from '@angular/core';
import {TreeNode} from 'primeng/api';
import {Tree} from 'primeng/tree';
import {Message} from 'primeng/components/common/api';
import { Router, ActivatedRoute } from '@angular/router';
import { EmitterVisitorContext } from '@angular/compiler/src/output/abstract_emitter';
import { TreeService } from 'src/pages/services/tree.service';



@Component({
    selector:'app-treedemo',
    templateUrl: './treedemo.html',
    styleUrls:['./treedemo.component.css']
})
export class TreeDemoComponent implements OnInit  {
    warnmsg: string='Please, select one widget first!';
    modalshow: boolean=true;
    warningshow: boolean =false;
    treeid:number;

    msgs: Message[];
    private treeData:any;
    @Input() private bgshow=false;
    @Input() private modalShow=false;
    @Input() private selectedFile:TreeNode;
    @Input() private widgetlist:any;
    @Input() private deleteshow=false;
    @Output() outdata= new EventEmitter<any>()
    @Output() modalclose=new EventEmitter<any>()
    @Output() chartype = new EventEmitter<any>()
    @Output() dataset = new EventEmitter<any>()          //双击节点获取数据集
    @Output() status = new EventEmitter<any>()
    @Output() nodedata=new EventEmitter<any>()
  
    
    constructor(
      private route: ActivatedRoute,
      private router:Router,
      private treeservice:TreeService
    ) {
      
    }

    ngOnInit(){
    
    }

  
    ngDoCheck(){
      console.log(this.widgetlist)
      this.treeData=this.widgetlist
    }

    ngAfterContentChecked(){
      
    }

    jstree_open_all(){
      this.modalshow=!this.modalshow
    }


    closemodal(){
      this.modalShow=false;
      this.modalclose.emit(this.modalShow);
      if(this.deleteshow){
        console.log(this.selectedFile.data.id)
        this.treeservice.deleteNode(this.selectedFile.data.id).then((res)=>{
          console.log(res)
          if(res.status==1){
            this.status.emit(res.status)
          }
        })
      }
    }

    // warning(){
    //     this.warningshow=true;
    //     if(!this.selectedFile){
    //       this.bgshow=true
    //     }else{
    //       this.bgshow=false
    //     }
    // }

    editnode(file){
      // console.log(file)
      this.router.navigate(['widget',file])
    }

    treenode:any;
    nodeSelect(data){
      this.treenode=data;
      console.log(data.node.id)
      this.outdata.emit(data.node.id);
      this.nodedata.emit(data.node)
      this.msgs = [];
      this.msgs.push({severity: 'info', summary: 'Node Selected', detail: data.node.label});
    }
    nodeUnselect(event) {
      this.msgs = [];
      this.msgs.push({severity: 'info', summary: '没有选择节点', detail: event.node.label});
    }
    toggle(file,ev){
      if(this.treenode.node.expanded){
        alert('不是最后一级')
      }else{
        console.log(file)
        
        // data.datasetid
        let nodedata =JSON.parse(this.treenode.node.data.extendInfo.data);
        console.log(nodedata.datasetId)       //undefined

        let data={scame:true,id:this.treenode.node.id,list:'edit'}
        this.outdata.emit(this.treenode.node.id)
        this.chartype.emit(data);
        this.dataset.emit(ev)
        this.treeservice.setData(ev);
        if(nodedata.datasetId){
          this.treeservice.setdataName(nodedata.datasetId)
        }else{
          alert('没有datasetId属性')
        }
        
      }
    }
}